﻿#include "FITKAcousticsHDF5Writer.h"
#include "FITKAcousticsHDF5IOInterface.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsMaterialsFluid.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsMaterialsIsosolid.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsMaterialsViscoElastic.h"

#include <H5Cpp.h>
#include <algorithm>


#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"
#include "FITKAcousticsPhysicsAdaptor.h"
#include "FITKAcousticsDiscModelAdaptor.h"


namespace IO
{
    FITKAcousticsHDF5Wriiter::FITKAcousticsHDF5Wriiter(FITKAcousticsHDF5IOInterface* interface, bool & isScuccess)
        :_isSuccess(isScuccess), _compInterface(interface)
    {
        if (_compInterface == nullptr) return;
        //声学离散模型  
        _discModel = _compInterface->getDataObjectT<Acoustics::FITKAcousticsDiscModel>("Model");

        //获取声学物理属性管理器
        _physics = _compInterface->getDataObjectT<Acoustics::FITKAcousticsPhysics>("Physics");
    }

    void FITKAcousticsHDF5Wriiter::run()
    {
        QString err;
        bool Result = this->createContext(err);//调用创建hdf5文件指针
        if (Result == false)
        {
            consoleMessage(3, "create hdf5 Context err");
            return;
        }
        if (_physics)
        {
            //调用适配器
            FITKAcousticsPhysicsAdaptor* adaptor =
                FITKIOADAPTORFACTORY->createT<FITKAcousticsPhysicsAdaptor>("FITKAcousticsHDF5", _physics);
            if (adaptor == nullptr) return;
            adaptor->setWriter(this);
            adaptor->setDataObject(_physics);//设置声适配器所需数据
            Result &= adaptor->adaptW();     //调用接口，写声学物理数据
            delete adaptor;                  //释放
        }
        
        if (_discModel != nullptr)
        {
            //调用适配器
            FITKAcousticsDiscModelAdaptor* adaptor =
                FITKIOADAPTORFACTORY->createT<FITKAcousticsDiscModelAdaptor>("FITKAcousticsHDF5", _discModel);
            if (adaptor == nullptr) return;
            adaptor->setWriter(this);
            adaptor->setDataObject(_discModel);//设置离散模型适配器所需数据
            Result &= adaptor->adaptW();       //调用接口，写离散模型数据
            delete adaptor;                    //释放
        }
    }

    void FITKAcousticsHDF5Wriiter::consoleMessage(int level, const QString& str)
    {
        switch (level)
        {
        case 1:AppFrame::FITKMessageNormal(str); break;
        case 2: AppFrame::FITKMessageWarning(str); break;
        case 3:AppFrame::FITKMessageError(str); break;
        default: AppFrame::FITKMessageError(str); break;
        }
    }
}



